﻿using Sidvall.Net;
using System.Threading.Tasks;

namespace Sidvall.Security
{
    public class AuthenticationStrategyManager
    {
        #region Public Members

        public Sidvall.Security.IAuthenticationStrategy AuthenticationStrategy { get; set; }

        #region CurrentAuthenticationHeader

        public AuthenticationHeader CurrentAuthenticationHeader
        {
            get
            {
                return this.AuthenticationStrategy?.CurrentAuthenticationHeader;
            }
            set
            {
                if (this.AuthenticationStrategy == null)
                    return;
                this.AuthenticationStrategy.CurrentAuthenticationHeader = value;
            }
        }

        #endregion
        #region TokenItem

        public TokenItem TokenItem
        {
            get
            {
                return this.AuthenticationStrategy?.TokenItem;
            }
        }

        #endregion

        #region LoadAuthenticationHeaderAsync

        public async Task LoadAuthenticationHeaderAsync(TokenResponse tokenResponse, bool storeInFile)
        {
            if (this.AuthenticationStrategy != null)
                await this.AuthenticationStrategy.LoadAuthenticationHeaderAsync(tokenResponse, storeInFile).ConfigureAwait(false);
        }

        #endregion
        #region LoadTokenItem

        public void LoadTokenItem(string content)
        {
            if (this.AuthenticationStrategy != null)
                this.AuthenticationStrategy.LoadTokenItem(content);
        }
        public void LoadTokenItem(TokenItem tokenItem)
        {
            if (this.AuthenticationStrategy != null)
                this.AuthenticationStrategy.LoadTokenItem(tokenItem);
        }

        #endregion
        #region LoadTokenItemAsync

        public async Task LoadTokenItemAsync(bool refresh)
        {
            if (this.AuthenticationStrategy != null)
                await this.AuthenticationStrategy.LoadTokenItemAsync(refresh).ConfigureAwait(false);
        }

        #endregion
        #region NewAuthenticationHeaderFromRefreshTokenAsync

        public async Task<bool> NewAuthenticationHeaderFromRefreshTokenAsync()
        {
            if (this.AuthenticationStrategy != null)
                return await this.AuthenticationStrategy.NewAuthenticationHeaderFromRefreshTokenAsync().ConfigureAwait(false);
            return false;
        }

        #endregion
        #region NewAuthenticationHeaderFromResourceOwnerPasswordAsync

        public async Task<bool> NewAuthenticationHeaderFromResourceOwnerPasswordAsync(string userName, string password, string scope)
        {
            if (this.AuthenticationStrategy != null)
                return await this.AuthenticationStrategy.NewAuthenticationHeaderFromResourceOwnerPasswordAsync(userName, password, scope).ConfigureAwait(false);
            return false;
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            return "AuthenticationStrategyManager - Sidvall.Security.IAuthenticationStrategy";
        }

        #endregion

        #endregion
        #region Constructors

        public AuthenticationStrategyManager(Sidvall.Security.IAuthenticationStrategy authenticationStrategy)
        {
            this.AuthenticationStrategy = authenticationStrategy;
        }

        #endregion
    }
}
